package com.jadesoft.stock;

import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.log4j.Logger;

import com.jadesoft.stock.entity.User;
import com.jadesoft.stock.util.Constants;
import com.jadesoft.stock.util.MD5;

public class MailBSImpl implements MailBS {
	private static Logger logger = Logger.getLogger(MailBSImpl.class);

	public void sendAuthMail(User user) throws EmailException {
		String to = user.getEmail();
		String name = StringUtils.isNotBlank(user.getName()) ? user.getName()
				: user.getEmail();
		String subject = "欢迎加入杰硕生活,快乐简单每一天!";
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String code = MD5.getMD5(user.getEmail() + user.getPassword());
		String url = Constants.DOMAIN + "/user.do?method=active&email="
				+ user.getEmail() + "&code=" + code;
		String content = "<div style='border-right:0px;padding-right:0px;border-top:0px;padding-left:0px;"
				+ "font-size:12px;padding-bottom:0px;margin:0px;border-left:0px;padding-top:0px;"
				+ "border-bottom:0px;text-align:center'>"
				+ "<div style=\"margin:0px auto;width:750px;text-align:left\">"
				+ "<div>"
				+ "<span></span>"
				+ "</div>"
				+ "</div>"
				+ "<div>"
				+ "<div>"
				+ "</div>"
				+ "<div>"
				+ "</div>"
				+ "<div>"
				+ "</div>"
				+ "<div>"
				+ "<div style=\"width:88%\">"
				+ "<span style=\"color:#003399;text-align:left\">"
				+ "<p>感谢你注册杰硕生活!<br></p>"
				+ "<p>"
				+ "<b><font color=\"red\">你的登录名为:"
				+ to
				+ "</font></b>"
				+ "</p>"
				+ "<p>"
				+ "请马上点击以下注册确认链接，激活你的杰硕生活帐号！"
				+ "</p>"
				+ "<p>"
				+ "<a href="
				+ url
				+ " target=\"_blank\"><strong>"
				+ url
				+ "</strong></a><br>"
				+ "<font style=\"color:red\">(该链接在48小时内有效，48小时后需要重新注册)</font>"
				+ "</p>"
				+ "<p>"
				+ "如果通过点击以上链接无法访问，<WBR>请将该网址复制并粘贴至新的浏览器窗口中。"
				+ "</p>"
				+ "<p>"
				+ "如果你错误地收到了此电子邮件，你无需执行任何操作来取消帐号！<WBR>此帐号将不会启动。"
				+ "</p>"
				+ "<br>"
				+ "<div style=\"border-top:1px dashed #cccccc;min-height:1px;overflow:hidden\"></div>"
				+ "<p>这只是一封系统自动发送的邮件，请不要直接回复。</p>"
				+ "<p align=\"right\">新浪生活</p>"
				+ "<p align=\"right\">"
				+ date
				+ "</p>" + "</span>" + "</div>";
		MailBSImpl.sendMail(to, name, subject, content);
	}

	public void sendActiveMail(User user) throws EmailException{
		String to = user.getEmail();
		String name = StringUtils.isNotBlank(user.getName()) ? user.getName()
				: user.getEmail();
		String subject = "欢迎加入杰硕生活,快乐简单每一天!激活成功";
		String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		String content = "<div style='border-right:0px;padding-right:0px;border-top:0px;padding-left:0px;"
				+ "font-size:12px;padding-bottom:0px;margin:0px;border-left:0px;padding-top:0px;"
				+ "border-bottom:0px;text-align:center'>"
				+ "<div style=\"margin:0px auto;width:750px;text-align:left\">"
				+ "<div>"
				+ "<span></span>"
				+ "</div>"
				+ "</div>"
				+ "<div>"
				+ "<div>"
				+ "</div>"
				+ "<div>"
				+ "</div>"
				+ "<div>"
				+ "</div>"
				+ "<div>"
				+ "<div style=\"width:88%\">"
				+ "<span style=\"color:#003399;text-align:left\">"
				+ "<p>感谢你注册杰硕生活!<br></p>"
				+ "<p>"
				+ "<b><font color=\"red\">你的登录名为:"
				+ to
				+ "</font></b>"
				+ "</p>"
				+ "<p>"
				+ "您已成功激活您的账号,您可以登录杰硕生活订阅免费短信服务。"
				+ "</p>"
				+ "<p>"
				+ "如果你错误地收到了此电子邮件，你无需执行任何操作来取消帐号！<WBR>此帐号将不会启动。"
				+ "</p>"
				+ "<br>"
				+ "<div style=\"border-top:1px dashed #cccccc;min-height:1px;overflow:hidden\"></div>"
				+ "<p>这只是一封系统自动发送的邮件，请不要直接回复。</p>"
				+ "<p align=\"right\">杰硕生活</p>"
				+ "<p align=\"right\">"
				+ date
				+ "</p>" + "</span>" + "</div>";
		logger.debug("send mail to "+to+subject);
		MailBSImpl.sendMail(to, name, subject, content);
	}

	public static void sendMail(String to, String toName, String subject,
			String content) {
		try {
			logger.info("start Mail " + to + "  " + subject);
			Properties props = new Properties();
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.user", "yangzt23@gmail.com");
			props.put("mail.smtp.password", "yangzhiting23");
			props.put("mail.smtp.port", "587"); // GMAIL SMTP port 587
			props.put("mail.smtp.auth", "true");
			String name = props.getProperty("mail.smtp.user");
			String password = props.getProperty("mail.smtp.password");
			String host = props.getProperty("mail.smtp.host");
			HtmlEmail email = new HtmlEmail();
			email.setHostName(host);// 设置发信的SMTP服务器
			email.setAuthentication(name, password);//
			email.setSSL(true);
			email.setSslSmtpPort("465");
			email.setCharset("utf-8");
			email.addTo(to, toName);// 设置收件人帐号和收件人
			email.setFrom("yangzt23@gmail.com", "杰硕生活");// 设置发信的邮件帐号和发信人
			email.setSubject(subject);// 设置邮件主题
			email.setHtmlMsg(content);// 设置邮件正文和字符编码
			email.setMsg(content);
			email.send();
			logger.debug("send message to " + to + "success" +subject);
		} catch (EmailException e) {
			logger.error(e);
		}
	}

	public static void main(String[] args) throws MalformedURLException,
			EmailException {
		User user = new User();
		user.setEmail("chunfengxixi@gmail.com");
		user.setName("杨之");
		MailBS mailBS = new MailBSImpl();
		mailBS.sendAuthMail(user);
	}
}
